import axios from 'axios';
import Vue from 'vue'

import { Notify } from 'vant';
import { errorr } from "./error"
import { errorren } from "./erroen"
Vue.use(Notify);
import {getCookie,delCookie,addCookie} from "./addCookie.js"
axios.defaults.timeout = 5000;
axios.defaults.baseURL ='http://api.staging.clockclub.one';


//http request 拦截器

    var ua = navigator.userAgent.toLowerCase();
	if(/iphone|ipad|ipod/.test(ua)) {	
	if((typeof jsObj)!="undefined"){	
		   var token=jsObj.getAppToken()
	  }else{
	  
	   }
   } else if(/android/.test(ua)) {					
	   if((typeof jsObj)!="undefined"){					   	
		  var token=jsObj.getAppToken()					  
	   }else{				   	
		 
	   }												    
	}else{
    var token ='eyJhbGciOiJIUzUxMiIsImlhdCI6MTU3NzY3MTA2MywiZXhwIjoxNTc4MTAzMDYzfQ.eyJpZCI6IjcxOGMzOGRlLTllZjMtNGE1MS04NjBjLTIyZTM3NTAxNGMyMSJ9.jDd51w0ap0SgvqVZ9shZsONkQuiZhzcyEHSIo62NQorX33vkSQ_8GWcrf6k4FoHQ4rNUzsbD3HeupjTpEPio8Q'				
	}
// var token = 'eyJhbGciOiJIUzUxMiIsImlhdCI6MTU3NzI2ODcyNCwiZXhwIjoxNTc3NzAwNzI0fQ.eyJpZCI6IjcxOGMzOGRlLTllZjMtNGE1MS04NjBjLTIyZTM3NTAxNGMyMSJ9.GLI9S3_WFMRZjg4nB4nJIFtDoLbrj4BFeOulFklFTAWZv3yZBJlfvwI8fwSHza_4D-YrHJFyZoxWQwSRYGapEQ'				

axios.interceptors.request.use(
  config => {
    config.data = JSON.stringify(config.data);
    config.headers = {
      "token":token
    }
    return config;
  },
  error => {
    return Promise.reject(err);
  }
);


//http response 拦截器
axios.interceptors.response.use(
  response => {
    
    
    // console.log(error.response.status)
    // alert(this.$t("message.global." + error.response.status + ""))
    if(response.data.errCode ==2){
      router.push({
        path:"/login",
        querry:{redirect:router.currentRoute.fullPath}//从哪个页面跳转
      })
    }
    return response;
  },
  error => {
    // alert(getCookie('lan'))
    var lanerr;
    if (getCookie('lan') =='zh'){
      lanerr = errorr
    }else{
      lanerr = errorren
    }
    // alert($t("message.global." + error.response.status + ""))
    if (error.response.status == 400) {
      Notify({
        message: lanerr[error.response.data.code],
        duration: 1000
      })
    }
    if (JSON.stringify(error.response.status) == 401) {     
      /*提示弹窗*/
      Notify({
        message: lanerr[error.response.status],
        duration: 1000
      })
    }



    return Promise.reject(error)
  }
)


/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url,params={}){
  return new Promise((resolve,reject) => {
    axios.get(url,{
      params:params
    })
    .then(response => {
      resolve(response.data);
    })
    .catch(err => {
      reject(err)
    })
  })
}
export function gett(url, params = {}) {
  
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params: params,
        responseType: 'blob'
      })
        .then(response => {
          resolve(response.data);
        })
        .catch(err => {
          reject(err)
        })
    })
  } 



/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

 export function post(url,data = {}){
   return new Promise((resolve,reject) => {
     axios.post(url,data)
          .then(response => {
            resolve(response);
          },err => {
            reject(err)
          })
   })
 }

 /**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.patch(url,data)
         .then(response => {
           resolve(response.data);
         },err => {
           reject(err)
         })
  })
}

 /**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.put(url,data)
         .then(response => {
           resolve(response.data);
         },err => {
           reject(err)
         })
  })
}